package com.luojun.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.luojun.entity.Car;
import com.luojun.entity.Commodity;
import com.luojun.entity.User;
import com.luojun.service.CarService;
import com.luojun.service.UCommodityService;
import com.sun.org.apache.xerces.internal.impl.io.UCSReader;

/**
 * Servlet implementation class DoAddCar
 */
@WebServlet("/DoAddCar")
public class DoAddCar extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoAddCar() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		int commodityid = Integer.parseInt(request.getParameter("commodityId"));
		UCommodityService ucs = new UCommodityService();
		Commodity commodity =ucs.findByCommodityId(commodityid);
		User user = (User)session.getAttribute("user");
		int userid = user.getUserId();
		Car car = new Car();
		car.setCommodity(commodity);
		car.setUser(user);
		car.setQuantity(1);
		CarService cs = new CarService();
		cs.save(car);
		request.getRequestDispatcher("ToIndex").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
